package ru.nosport.matrixaria.modules.user.rmreg.server;

import ru.nosport.matrixaria.modules.common.AppServerProcess;
import ru.nosport.matrixaria.modules.user.rmreg.client.Needle;
import ru.nosport.matrixaria.core.MessageHandler;
import ru.nosport.matrixaria.core.MessageHandlerException;
import org.apache.commons.configuration.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * User: Victor Fabrichenko
 * Date: 28.04.2009
 * Time: 16:27:50
 */
public class CushionApp implements Cushion, AppServerProcess {
	static public final String testPhrase = "stuck_needle.";
	private String remoteNeedleName;
	private String resultString;
	static public final String testColor = "red";

	public CushionApp(Configuration _conf){

	}

	public String stuckNeedle(String _moduleName){
		this.remoteNeedleName = _moduleName;
		return testPhrase + this.remoteNeedleName;
	}

	public String getRezForTest() {
		return this.resultString;
	}

	public boolean moduleStart() throws Exception {

		Thread t = new Thread(new Runnable(){
			public void run(){
				while(true){
					if(remoteNeedleName != null){
						try {
							resultString = (String)MessageHandler.sendMessage(remoteNeedleName, "toThreadNeedle", new Object[]{testColor});
						} catch (MessageHandlerException e) {
							e.printStackTrace();
						}
						break;
					}
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (InterruptedException e) {}
				}
			}
		});
		t.start();

		return true;
	}

	public boolean moduleStop() {
		return true;
	}

	public boolean isModuleRunning() {
		return false;
	}

	public String moduleStat() {
		return null;
	}
}
